<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function longestPalindrome(s) {
            let n = s.length;
            let res = '';
            let dp = Array.from({ length: n }, () => Array(n).fill(false));

            for (let i = n - 1; i >= 0; i--) {
                for (let j = i; j < n; j++) {
                    dp[i][j] = s[i] == s[j] && (j - i < 3 || dp[i + 1][j - 1]);
                    if (dp[i][j] && j - i + 1 > res.length) {
                        res = s.substring(i, j + 1);
                    }
                }
            }
            return res;
        }
        console.log(longestPalindrome("babad"))
    </script>
</body>

</html>